<template>
  <div class="login">
    <el-form
      class="form"
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
    >
      <el-form-item>
        <div class="login-title-content">
          <div class="title">{{ $t('login.title') }}</div>
          <LocaleDropdown />
        </div>
      </el-form-item>
      <el-form-item :label="$t('login.username')" prop="username">
        <el-input v-model="ruleForm.username" />
      </el-form-item>
      <el-form-item :label="$t('login.password')" prop="password">
        <el-input type="password" show-password v-model="ruleForm.password" />
      </el-form-item>
      <el-form-item>
        <el-button class="btn" type="primary" @click="submitForm(ruleFormRef)">
          {{ $t('login.login') }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { loginApi } from '@/api/login'
import { useUserStore } from '@/stores/modules/user'
import { useI18n } from 'vue-i18n'
import LocaleDropdown from '@/components/LocaleDropdown.vue'
interface RuleForm {
  username: string
  password: string
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  username: '',
  password: '',
})
const { t, locale } = useI18n()
const rules = reactive<FormRules<RuleForm>>({})
// 根据当前语言变化更新规则
watch(
  () => locale.value,
  () => {
    Object.assign(rules, {
      username: [{ required: true, message: t('login.usernameRequire'), trigger: 'blur' }],
      password: [
        {
          required: true,
          message: t('login.passwordRequire'),
          trigger: 'blur',
        },
      ],
    })
  },
  {
    immediate: true, // 立即执行，否则没有规则
  },
)
const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const res = await loginApi({
        ...ruleForm,
      })
      if (res?.code === 0) {
        userStore.setToken(res.data.token)
        userStore.setUserInfo({
          username: res.data.username,
        })
        if (route.query.redirect) {
          router.push(route.query.redirect as string)
        } else {
          router.push('/')
        }
      }
    }
  })
}
</script>
<style lang="scss" scoped>
.login {
  background-color: #2c3e50;
  width: 100%;
  height: 100%;
  position: relative;
  .login-title-content {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
  .title {
    text-align: center;
    font-size: 22px;
    font-weight: 700;
    line-height: 40px;
  }
  .form {
    width: 400px;
    background-color: #fff;
    padding: 20px 30px;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .btn {
    width: 100%;
  }
}
</style>
